﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Schema;

namespace ConsoleApp10
{
    public class OrderService
    {
        public List<Order> orders;
        private Customer user;
        public string Buyer { get => user.Name; set { user = new Customer(value); } }
        public OrderService()
        {
            orders = new List<Order>();
            user = new Customer("");
        }
        public void AddOrder(OrderDetails orderDetails)
        {
            int length = orders.Count;
            Order order = new Order(length + 1, user, orderDetails);
            if (length == 0)
            {
                orders.Add(order);
                Console.WriteLine("订单添加成功");
            }
            else
            {
                for (int i = 0; i < length; i++)
                {
                    if (order.Equals(orders[i]))
                    {
                        Console.WriteLine("订单添加失败");
                        break;
                    }
                    else if (i == length - 1)
                    {
                        orders.Add(order);
                        Console.WriteLine("订单添加成功");
                    }
                }
            }
        }
        private void IsLegal(int n)
        {
            if (n < 1 || n > orders.Count)
                throw new Exception("无效账单号");
        }
        public void DelOrder(int id)
        {
            try
            {
                IsLegal(id);
                orders.Remove(orders[id - 1]);
                for (int i = 0; i < orders.Count; i++)
                    orders[i].Id = i + 1;
                Console.WriteLine("删除成功");
            }
            catch(Exception e)
            {
                Console.WriteLine("删除失败："+e.Message);
            }
        }
        public void EditOrder(int id,string name,int num)
        {
            try
            {
                IsLegal(id);
                orders[id - 1].EditDetails(name, num);
                Console.WriteLine("修改成功");
            }
            catch (Exception e)
            {
                Console.WriteLine("修改失败：" + e.Message);
            }
        }
        public void FindOrders(int id)
        {
            var query = from s in orders where s.Id == id orderby s.Cost select s;
            if (query.Count() == 0)
                Console.WriteLine("未找到符合条件的订单");
            else
            {
                foreach (Order o in query)
                    Console.WriteLine(o.ToString());
            }
        }
        public void FindOrders(string name)
        {
            var query = from s in orders where s.FindGoods(name) == false orderby s.Cost select s;
            if (query.Count() == 0)
                Console.WriteLine("未找到符合条件的订单");
            else
            {
                foreach (Order o in query)
                    Console.WriteLine(o.ToString());
            }
        }
        public void FindOrders(Customer owner)
        {
            var query = from s in orders where s.Customer == owner.Name orderby s.Cost select s;
            if (query.Count() == 0)
                Console.WriteLine("未找到符合条件的订单");
            else
            {
                foreach (Order o in query)
                    Console.WriteLine(o.ToString());
            }
        }
        public void FindOrdersLess(int n)
        {
            var query = from s in orders where s.Cost <= n orderby s.Cost select s;
            if (query.Count() == 0)
                Console.WriteLine("未找到符合条件的订单");
            else
            {
                foreach (Order o in query)
                    Console.WriteLine(o.ToString());
            }
        }
        public void FindOrdersAbove(int n)
        {
            var query = from s in orders where s.Cost >= n orderby s.Cost select s;
            if (query.Count() == 0)
                Console.WriteLine("未找到符合条件的订单");
            else
            {
                foreach (Order o in query)
                    Console.WriteLine(o.ToString());
            }
        }
        public void SortOrders(Action<Order, Order> act = null)
        {
            int length = orders.Count();
            List<Order> newlist = new List<Order>();
            if (act == null)
            {
                act = ((a, b) => { if (a.Id > b.Id) a.Swap(b); });
            }
            for (int i = 0; i < length - 1; i++)
                for (int j = 0; j < length - 1 - i; j++)
                    act(orders[j], orders[j + 1]);
            newlist = orders;
            orders = newlist;
        }
    }
}
